/**
 * 项目名称（软件名称）：忠旺机械物资管理系统(mms)
 * 研发设计单位：忠旺机械设备制造有限公司机械设计院智能控制设计科设计与研发。
 * 版权所有：忠旺机械设备制造有限公司
 * 注：本项目的一切技术专利，使用规范归解释权由忠旺机械设备制造有限公司所有。
 * 软件版本：Ver 1.0.0
 * 文件路径：com.zhongwang.mms.module.purchase.controller
 * 创建时间：2020年04月24日 18时11分
 **/
package com.zhongwang.mms.module.purchase.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.zhongwang.mms.base.BaseController;
import com.zhongwang.mms.common.JsonResult;
import com.zhongwang.mms.module.purchase.model.VWProject;
import com.zhongwang.mms.module.purchase.model.check.*;
import com.zhongwang.mms.module.purchase.service.PurCheckService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName： CheckController
 * @Description： 盘点控制层
 * @Author： 徐一贺
 * @Date： 2020/4/24 18:11
 * @Version： V1.0
 **/
@RequestMapping("/purchase/check")
@Controller
public class PurCheckController extends BaseController {

    @Autowired
    private PurCheckService purCheckService;

    /**
     * @FunctionName: plan
     * @Description: 跳转到盘点计划
     * @Author: 徐一贺
     * @CreateDate: 2020/4/24 18:16
     * @Phone: 18241927380
     * @Version: 1.0.0
     */
    @GetMapping
    @RequiresPermissions({"purinv:purcheckplan:view"})
    public String plan() {
        return "/purchase/purInvManage/check/plan/page";
    }

    @GetMapping("/detail/{cpnId}")
    @RequiresPermissions({"purinv:purcheckadd:view"})
    /**
     *@FunctionName: detail
     *@Description: 跳转到添加明细页
     *@Author: 徐一贺
     *@CreateDate: 2020/4/25 13:55
     *@Phone: 18241927380
     *@Param: [cpnId] 主键 [model] 渲染模型
     *@Return: java.lang.String
     *@Version: 1.0.0
     */
    public String detail(Model model, @PathVariable("cpnId") Integer cpnId) {
        model.addAttribute("cpnId", cpnId);
        model.addAttribute("mode", "add");
        model.addAttribute("cpnCode", purCheckService.getCheckPlanByPK(cpnId).getCpnCode());
        return "/purchase/purInvManage/check/detail/page";
    }

    @GetMapping("/detail/view/{cpnId}")
    @RequiresPermissions({"purinv:purcheckadd:view"})
    /**
     *@FunctionName: detail
     *@Description: 跳转到添加明细页
     *@Author: 徐一贺
     *@CreateDate: 2020/4/25 13:55
     *@Phone: 18241927380
     *@Param: [cpnId] 主键 [model] 渲染模型
     *@Return: java.lang.String
     *@Version: 1.0.0
     */
    public String detailView(Model model, @PathVariable("cpnId") Integer cpnId) {
        model.addAttribute("cpnId", cpnId);
        model.addAttribute("mode", "view");
        model.addAttribute("cpnCode", purCheckService.getCheckPlanByPK(cpnId).getCpnCode());
        return "/purchase/purInvManage/check/detail/page";
    }

    @GetMapping("/plan/dispatch/{mode}/{cpnId}")
    /**
     *@FunctionName: detail
     *@Description: 页面调度中心
     *@Author: 徐一贺
     *@CreateDate: 2020/4/25 13:55
     *@Phone: 18241927380
     *@Param: [cpnId] 主键 [model] 渲染模型
     *@Return: java.lang.String
     *@Version: 1.0.0
     */
    public String dispatch(Model model, @PathVariable("mode") String mode, @PathVariable("cpnId") Integer cpnId) {
        PCheckPlanModel checkPlanModel = purCheckService.getCheckPlanByPK(cpnId);
        model.addAttribute("checkPlanModel", checkPlanModel);
        if (mode.equals("result")) {
            return "/purchase/purInvManage/check/result/page";
        }
        if (mode.equals("adjust")) {
            return "/purchase/purInvManage/check/adjust/page";
        }
        if (mode.equals("pal")) {
            return "/purchase/purInvManage/check/pal/page";
        }
        return "/purchase/purInvManage/check/detail/page";
    }

    @PostMapping("/plan/list")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckplan:view"})
    /**
     *@FunctionName: planList
     *@Description: 盘点计划列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/24 19:07
     *@Phone: 18241927380
     *@Param: [param] 检索参数
     *@Return: com.zhongwang.mms.common.JsonResult 列表数据
     *@Version: 1.0.0
     */
    public JsonResult planList(PCheckPlanQueryModel param) {
        startPage();
        List<PCheckPlanModel> list = purCheckService.findPlanList(param);
        return jsonResult(list);
    }

    @PostMapping("/result/list")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckresult:view"})
    /**
     *@FunctionName: resultList
     *@Description: 盘点结果列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/24 19:52
     *@Phone: 18241927380
     *@Param: [param] 检索参数
     *@Return: com.zhongwang.mms.common.JsonResult 列表数据
     *@Version: 1.0.0
     */
    public JsonResult resultList(PCheckDetailQueryModel param) {
        startPage();
        List<PCheckDetailModel> list = purCheckService.findResultList(param);
        return jsonResult(list);
    }

    @PostMapping("/adjust/list")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckadj:view"})
    /**
     *@FunctionName: adjustList
     *@Description: 盘点损益调整列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/29 09:28
     *@Phone: 18241927380
     *@Param: [param] 检索参数
     *@Return: com.zhongwang.mms.common.JsonResult 列表数据
     *@Version: 1.0.0
     */
    public JsonResult adjustList(PCheckDetailQueryModel param) {
        startPage();
        List<PCheckDetailModel> list = purCheckService.findAdjustList(param);
        return jsonResult(list);
    }

    @PostMapping("detail/list")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckadd:view"})
    /**
     *@FunctionName: detailList
     *@Description: 根据盘点计划编码获取盘点明细列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/24 19:07
     *@Phone: 18241927380
     *@Param: [param] 检索参数
     *@Return: com.zhongwang.mms.common.JsonResult 列表数据
     *@Version: 1.0.0
     */
    public JsonResult detailList(PCheckDetailQueryModel param) {
        startPage();
        List<PCheckDetailModel> list = purCheckService.findDetailList(param);
        return jsonResult(list);
    }

    @PostMapping("detail/create")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckadd:edit"})
    /**
     *@FunctionName: detailCreate
     *@Description: 根据传过来的库存主键创建盘点明细
     *@Author: 徐一贺
     *@CreateDate: 2020/4/24 19:07
     *@Phone: 18241927380
     *@Param: [param] 前端传递过来的参数 盘点计划编号和库存信息主键
     *@Return: com.zhongwang.mms.common.JsonResult 列表数据
     *@Version: 1.0.0
     */
    public JsonResult detailCreate(PCheckDetailQueryModel param) {
        return purCheckService.detailCreate(param);
    }


    @PostMapping("/plan/save")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckplan:edit"})
    /**
     *@FunctionName: planSave
     *@Description: 保存计划
     *@Author: 徐一贺
     *@CreateDate: 2020/4/25 10:21
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult planSave(PCheckPlanModel param) {
        boolean result = purCheckService.planSave(param);
        if (result) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @PostMapping("/plan/remove")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckplan:edit"})
    /**
     *@FunctionName: planRemove
     *@Description: 删除（逻辑）盘点计划
     *@Author: 徐一贺
     *@CreateDate: 2020/4/25 11:30
     *@Phone: 18241927380
     *@Param: [param] 盘点计划主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult planRemove(PCheckPlanModel param) {
        boolean result = purCheckService.planRemove(param);
        if (result) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @PostMapping("/detail/remove")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckadd:edit"})
    /**
     *@FunctionName: detailRemove
     *@Description: 删除（物理）盘点明细
     *@Author: 徐一贺
     *@CreateDate: 2020/4/25 11:30
     *@Phone: 18241927380
     *@Param: [param] 盘点明细主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult detailRemove(PCheckDetailModel param) {
        boolean result = purCheckService.detailRemove(param);
        if (result) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }
    @PostMapping("/detail/removes")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckadd:edit"})
    /**
     *@FunctionName: detailRemoves
     *@Description: 批量删除（物理）盘点明细
     *@Author: 徐一贺
     *@CreateDate: 2020/5/4 15:59
     *@Phone: 18241927380
     *@Param: [param] 盘点明细主键们
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult detailRemoves(PCheckDetailModel param) {
        return purCheckService.detailRemoves(param);
    }

    @PostMapping("/plan/submit")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckplan:edit"})
    /**
     *@FunctionName: planSubmit
     *@Description: 提交盘点计划
     *@Author: 徐一贺
     *@CreateDate: 2020/4/25 11:30
     *@Phone: 18241927380
     *@Param: [param] 盘点计划主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult planSubmit(PCheckPlanModel param) {
        boolean result = purCheckService.planSubmit(param);
        if (result) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @PostMapping("/plan/revoke")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckplan:edit"})
    /**
     *@FunctionName: planRevoke
     *@Description: 撤销盘点计划
     *@Author: 徐一贺
     *@CreateDate: 2020/4/25 11:30
     *@Phone: 18241927380
     *@Param: [param] 盘点计划主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult planRevoke(PCheckPlanModel param) {
        return purCheckService.planRevoke(param);
    }

    @PostMapping("/result/submit")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckresult:edit"})
    /**
     *@FunctionName: resultSubmit
     *@Description: 提交盘点结果
     *@Author: 徐一贺
     *@CreateDate: 2020/4/28 19:51
     *@Phone: 18241927380
     *@Param: [param] 盘点结果
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult resultSubmit(PCheckDetailModel param) {
        return purCheckService.resultSubmit(param.getCdlCpnId());

    }

    @PostMapping("/adjust/submit")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckadj:edit"})
    /**
     *@FunctionName: adjustSubmit
     *@Description: 提交盘点损益调整
     *@Author: 徐一贺
     *@CreateDate: 2020/4/29 11:14
     *@Phone: 18241927380
     *@Param: [param] 盘点计划主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult adjustSubmit(PCheckDetailModel param) {
        return purCheckService.adjustSubmit(param.getCdlCpnId());
    }


    @PostMapping("/adjust/revoke")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckadj:edit"})
    /**
     *@FunctionName: adjustRevoke
     *@Description: 撤销盘点损益调整
     *@Author: 徐一贺
     *@CreateDate: 2020/4/29 12:28
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult adjustRevoke(PCheckDetailModel param) {
        return purCheckService.adjustRevoke(param.getCdlCpnId());
    }

    @PostMapping("/result/revoke")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckresult:edit"})
    /**
     *@FunctionName: resultSubmit
     *@Description: 撤销盘点结果
     *@Author: 徐一贺
     *@CreateDate: 2020/4/28 19:51
     *@Phone: 18241927380
     *@Param: [param] 撤销结果
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult resultRevoke(PCheckDetailModel param) {
        boolean result = purCheckService.resultRevoke(param.getCdlCpnId());
        if (result) {
            return JsonResult.success("提交盘点结果成功！");
        }
        return JsonResult.fail("提交盘点结果失败");
    }

    @PostMapping("/result/check")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckresult:edit"})
    /**
     *@FunctionName: resultCheck
     *@Description: 单条盘点
     *@Author: 徐一贺
     *@CreateDate: 2020/4/27 9:59
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult resultCheck(PCheckDetailQueryModel param) {
        if (purCheckService.resultCheck(param)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @PostMapping("/result/checkAdd")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckresult:newplan"})
    /**
     *@FunctionName: checkAdd
     *@Description: 新增盘点
     *@Author: 徐一贺
     *@CreateDate: 2020/4/27 15:09
     *@Phone: 18241927380
     *@Param: [param] 新增盘点的信息
     *@Return: com.zhongwang.mms.common.JsonResult 添加结果
     *@Version: 1.0.0
     */
    public JsonResult checkAdd(PCheckDetailModel param) {
        if (purCheckService.checkAdd(param)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }

    @PostMapping("/result/codeExist")
    @ResponseBody
    /**
     *@FunctionName: codeExist
     *@Description: bootstrapValidate验证：验证项目号是否存在
     *@JavaScriptDemo： notEmpty: {message: '请输入需求项目编号'},
     *                         threshold: 6, //有6字符以上才发送ajax请求，（input中输入一个字符，插件会向服务器发送一次，设置限制，6字符以上才开始）
     *                         remote: {//ajax验证。server result:{"valid",true or false} 向服务发送当前input name值，获得一个json数据。例表示正确：{"valid",true}
     *                             url: 'purchase/change/codeExist',//验证地址
     *                             message: '用户已存在',//提示消息
     *                             delay: 100,//每输入一个字符，就发ajax请求，服务器压力还是太大，设置2秒发送一次ajax（默认输入一个字符，提交一次，服务器压力太大）
     *                             type: 'POST'//请求方式
     *                             /**自定义提交数据，默认值提交当前input value
     *                              *  data: function(validator) {
     *                                return {
     *                                    password: $('[name="passwordNameAttributeInYourForm"]').val(),
     *                                    whatever: $('[name="whateverNameAttributeInYourForm"]').val()
     *                                };
     *                             }
     *
     *
     *@Author: 徐一贺
     *@CreateDate: 2020/4/18 13:24
     *@Phone: 18241927380
     *@Param: [param] 项目号
     *@Return: java.lang.Boolean
     *@Version: 1.0.0
     */
    public String codeExist(VWProject param) {
        Map<String, Boolean> map = new HashMap<String, Boolean>();
        map.put("valid", purCheckService.codeExist(param));
        String resultString = "";
        ObjectMapper mapper = new ObjectMapper();
        try {
            resultString = mapper.writeValueAsString(map);
        } catch (JsonProcessingException e) {
            e.printStackTrace();
        }
        return resultString;
    }


    @ResponseBody
    @GetMapping("/result/export/{cpnId}")
    @RequiresPermissions({"purinv:purcheckresult:edit"})
    /**
     *@FunctionName: resultExport
     *@Description: 导出盘点计划
     *@Author: 徐一贺
     *@CreateDate: 2020/4/27 16:48
     *@Phone: 18241927380
     *@Param: [response]
     *@Return: void
     *@Version: 1.0.0
     */
    public void resultExport(HttpServletResponse response, @PathVariable("cpnId") Integer cpnId) {
        purCheckService.planExport(response, cpnId);
    }

    @ResponseBody
    @GetMapping("/adjust/export/{cpnId}")
    @RequiresPermissions({"purinv:purcheckadj:export"})
    /**
     *@FunctionName: adjustExport
     *@Description: 导出盘点损益调整计划
     *@Author: 徐一贺
     *@CreateDate: 2020/4/29 10:11
     *@Phone: 18241927380
     *@Param: [response]
     *@Return: void
     *@Version: 1.0.0
     */
    public void adjustExport(HttpServletResponse response, @PathVariable("cpnId") Integer cpnId) {
        purCheckService.adjustExport(response, cpnId);
    }

    @PostMapping("/result/clean")
    @ResponseBody
    @RequiresPermissions({"purinv:purcheckresult:edit"})
    /**
     *@FunctionName: resultClean
     *@Description: 清空该计划的盘点结果
     *@Author: 徐一贺
     *@CreateDate: 2020/4/28 10:37
     *@Phone: 18241927380
     *@Param: [param] 计划主键
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult resultClean(PCheckDetailQueryModel param) {
        if (purCheckService.resultClean(param)) {
            return JsonResult.success();
        }
        return JsonResult.fail();
    }


    @ResponseBody
    @PostMapping("result/import")
    @RequiresPermissions({"purinv:purcheckresult:edit"})
    /**
     *@FunctionName: resultImport
     *@Description: 盘点结果导入
     *@Author: 徐一贺
     *@CreateDate: 2020/4/28 12:36
     *@Phone: 18241927380
     *@Param: [file] 盘点结果Excel文件
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult resultImport(@RequestParam("file") MultipartFile file) {
        return success(purCheckService.resultImport(file));
    }

    @ResponseBody
    @PostMapping("adjust/edit")
    @RequiresPermissions({"purinv:purcheckadj:edit"})
    /**
     *@FunctionName: adjustEdit
     *@Description: 调整损益差和备注
     *@Author: 徐一贺
     *@CreateDate: 2020/4/29 11:02
     *@Phone: 18241927380
     *@Param: [param] 调整损益差和备注的参数
     *@Return: [JsonResult] 页面结果
     *@Version: 1.0.0
     */
    public JsonResult adjustEdit(PCheckDetailModel param) {
        return purCheckService.adjustEdit(param);
    }

    @PostMapping("/pal/list")
    @ResponseBody
    @RequiresPermissions({"purinv:purchecksub:view"})
    /**
     *@FunctionName: palList
     *@Description: 获得盘点损益表列表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/29 14:10
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult palList(PAdjustmentApplyModel param) {
        return jsonResult(purCheckService.palList(param));
    }

    @PostMapping("/pal/sumList")
    @ResponseBody
    @RequiresPermissions({"purinv:purchecksub:view"})
    /**
     *@FunctionName: palSumList
     *@Description: 获取汇总表信息
     *@Author: 徐一贺
     *@CreateDate: 2020/4/29 15:58
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult palSumList(PAdjustmentApplyModel param) {
        return jsonResult(purCheckService.palSumList(param));
    }
    @PostMapping("/pal/submit")
    @ResponseBody
    @RequiresPermissions({"purinv:purchecksub:edit"})
    /**
     *@FunctionName: palSubmit
     *@Description: 提交损益表
     *@Author: 徐一贺
     *@CreateDate: 2020/4/29 16:58
     *@Phone: 18241927380
     *@Param: [param]
     *@Return: com.zhongwang.mms.common.JsonResult
     *@Version: 1.0.0
     */
    public JsonResult palSubmit(PAdjustmentApplyModel param) {
        return purCheckService.palSubmit(param);
    }




}